package genericity;

import java.util.Arrays;
import java.util.List;

// T extends Number: T的类型可以是Number以及子类
class Calculator<T extends Number>{
	public T add(T a,T b) {
	    return a;
	}
}

// ? super Integer: 只能是Integer以及其父类
// ? extends Number :只能是Number以及子类
class Calculator2{
	public void test(List<? super Integer> list) {
		list.forEach(System.out::println);
	}
	public void test2(List<? extends Number> list) {
		list.forEach(System.out::println);
	}
}

public class GenerictityDemo {
	public static void main(String[] args) {
		/**
		 * class Calculator{
				public Integer add(Integer a,Integer b) {
				    return a;
				}
			}
		 */
		
		// jdk1.8以后后面的泛型可以推导
		Calculator<Integer> c1 = new Calculator<>();
		c1.add(1, 2);
		
		
		/**
		 * class Calculator{
				public Double add(Double a,Double b) {
				    return a;
				}
			}
		 */
		Calculator<Double> c2 = new Calculator<>();
		c2.add(1.1, 2.2);
		
		Calculator2 cc2 = new Calculator2();
		List<Integer> list = Arrays.asList(1,2,3);
		cc2.test(list);
	}
}
